using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using TeamXpandLicence.ObjectExchange;
using TXLooker.Properties;

namespace TXLooker.License
{
    public class FullLicenceCheck : ILicenceCheck
    {
        private FullLicenceCheck()
        { }

        public FullLicenceCheck(string licenceKey)
            : this()
        {
            this.LicenceKey = licenceKey;
            CheckLicence();
        }

        private string m_licenceKey;
        public string LicenceKey
        {
            get { return this.m_licenceKey; }
            set { this.m_licenceKey = value; }
        }

        private int m_daysOff;
        public int DaysOff
        {
            get { return this.m_daysOff; }
            set { this.m_daysOff = value; }
        }

        private LicenceType m_licenceType;
        public LicenceType LicenceType
        {
            get { return this.m_licenceType; }
            set { this.m_licenceType = value; }
        }

        public void CheckLicence()
        {
            try
            {
                string sendString = string.Empty;
                using (MemoryStream MS = new MemoryStream())
                {
                    XmlSerializer formatter = new XmlSerializer(typeof(FullLicenceDataObjectIn));
                    FullLicenceDataObjectIn objectIn = new FullLicenceDataObjectIn(ApplicationGlobal.MacAddress, this.LicenceKey);

                    formatter.Serialize(MS, objectIn);
                    byte[] buffer = MS.GetBuffer();
                    sendString = Encoding.UTF8.GetString(buffer);
                }
                FullLicenceDataObjectOut objOut = null;
                string requestString = Resources.STRING_LICENCE_URL + "CheckFullLicence?FullLicenceDataObjectIn=" + sendString;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestString);
                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        byte[] buffer = new byte[webResponse.ContentLength];

                        stream.Read(buffer, 0, buffer.Length);
                        string retString = Encoding.UTF8.GetString(buffer);
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(retString);
                        retString = doc.InnerText;

                        using (TextReader textReader = new StringReader(retString))
                        {
                            XmlSerializer formatter = new XmlSerializer(typeof(FullLicenceDataObjectOut));
                            objOut = (FullLicenceDataObjectOut)formatter.Deserialize(textReader);
                        }
                    }
                }
                if (objOut == null)
                {
                    this.DaysOff = -1;
                    this.LicenceType = LicenceType.Error;
                }
                this.LicenceType = objOut.LicenceType;
                this.DaysOff = objOut.TimeLicenceLeft;
                if (this.DaysOff < 0)
                    this.LicenceType = LicenceType.FullDaysOff;
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.STRING_UNABLE_CONNECT_TO_SERVER_ADND_GET_DATA,Resources.APPLICATION_NAME,MessageBoxButtons.OK,MessageBoxIcon.Stop);
                this.DaysOff = -1;
                this.LicenceType = LicenceType.Error;
            }
        }
    }
}
